<template>
  <div class="row col-12">
    <div class="col-6">
      <div class="card">
        <div class="card-body">未分组客户</div>
        <div class="card-body">
          <form class="row">
            <div class="col-2">
              <input
                type="textfield"
                class="form-control"
                placeholder="公司/编号"
                v-model.trim="name0"
                size="10"
              />
            </div>
            <div class="col">
              <button
                type="button"
                class="btn btn-primary ms-1"
                @click="search(0)"
              >
                查找
              </button>
            </div>
          </form>
        </div>
        <table class="table table-sm table-striped">
          <thead>
            <tr>
              <th>企业名称</th>
              <th>最近购买</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="info of customers">
              <td>{{ info.vipName }}</td>
              <td>{{ info.recency }}</td>
              <td>
                <input
                  type="radio"
                  v-model.number="customerId"
                  :value="info.id"
                />
              </td>
            </tr>
          </tbody>
        </table>
        <div class="card-body my-0 py-0">
          <select v-model.number="groupId">
            <option :value="info.id" v-for="info of groups">
              {{ info.groupName }}
            </option>
          </select>
          <button
            id="btnSave"
            type="button"
            @click.stop="saveCustomerGroup()"
            class="btn btn-sm btn-primary"
          >
            保存分组
          </button>
        </div>
        <nav id="pagination-box" class="float-end">
          <my-pagination
            name="pagination0"
            :row-count="sc0.rowCount"
            :page-total="sc0.pageTotal"
            :page-no="sc0.pageNo"
            @next-page="nextPage0"
            @prev-page="prevPage0"
            @direct-page="directPage0"
          ></my-pagination>
        </nav>
      </div>
    </div>
    <div class="col-6">
      <div class="card">
        <div class="card-body">已分组客户</div>
        <div class="card-body">
          <form class="row">
            <div class="col-2">
              <input
                type="textfield"
                class="form-control"
                placeholder="公司/编号"
                v-model.trim="name1"
                size="10"
              />
            </div>
            <div class="col">
              <button
                type="button"
                class="btn btn-primary ms-1"
                @click="search(1)"
              >
                查找
              </button>
            </div>
          </form>
        </div>
        <table class="table table-sm">
          <thead>
            <tr>
              <th>企业名称</th>
              <th>最近购买</th>
              <th>分组信息</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="info of groupedCustomers">
              <td>{{ info.vipName }}</td>
              <td>{{ info.recency }}</td>
              <td>{{ info.groupName }}</td>
              <td>
                <button @click.stop="removeCustomerGroup(info.id)">
                  从分组移除
                </button>
              </td>
            </tr>
          </tbody>
        </table>
        <nav id="pagination-box" class="float-end">
          <my-pagination
            name="pagination1"
            :row-count="sc1.rowCount"
            :page-total="sc1.pageTotal"
            :page-no="sc1.pageNo"
            @next-page="nextPage1"
            @prev-page="prevPage1"
            @direct-page="directPage1"
          ></my-pagination>
        </nav>
      </div>
    </div>
  </div>
</template>

<script>
import { searchCustomerDetails } from "@/api/customer.js";
import { saveCustomerGroup, removeCustomerGroup } from "@/api/admin.js";
import MyPagination from "@/components/my-pagination.vue";

export default {
  components: {
    MyPagination,
  },
  props: {
    groups: {
      type: Array,
    },
  },
  data() {
    return {
      sc0: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      sc1: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },

      modalTitle: "",
      saving: false,

      customers: [],
      groupedCustomers: [],

      customerId: 0,
      groupId: 0,

      name0: "",
      name1: "",
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
  },
  mounted: function () {
    this.search(0);
    this.search(1);
  },
  activated: function () {
    consle.log("custome-travel-zone: activated");
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (msg) {
      this.$store.dispatch("showLoading", { loadingText: msg });
    },
    hideLoading: function () {
      this.$store.dispatch("hideLoading");
    },
    search: function (grouped) {
      if (grouped === 0) {
        this.searchUngrouped();
      } else if (grouped === 1) {
        this.searchGrouped();
      }
    },
    searchGrouped: function () {
      const params = {
        "sc.pageNo": this.sc1.pageNo,
        "sc.pageSize": this.sc1.pageSize,
        "sc.name": this.name1,
        "sc.orderBy": 3,
        "sc.status": 1,
        "sc.grouped": 1,
      };
      searchCustomerDetails(params, (v) => {
        this.groupedCustomers = v.dataList;
        this.sc1 = v.page;
      });
    },
    searchUngrouped: function () {
      const params = {
        "sc.pageNo": this.sc0.pageNo,
        "sc.pageSize": this.sc0.pageSize,
        "sc.name": this.name0,
        "sc.orderBy": 3,
        "sc.status": 1,
        "sc.grouped": 0,
      };
      searchCustomerDetails(params, (v) => {
        this.customers = v.dataList;
        this.sc0 = v.page;
      });
    },
    saveCustomerGroup: function () {
      const params = {
        customerId: this.customerId,
        groupId: this.groupId,
      };

      // const jsonParam = JSON.stringify(params)

      saveCustomerGroup(params, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("保存成功");
          this.search(0);
          this.search(1);
          if (this.keepOpening === false) {
            this.reset();
          }
        } else {
          this.showErrMsg("保存失败：" + v.errmsg, "danger");
        }
      });
    },
    removeCustomerGroup: function (customerId) {
      const params = {
        customerId: customerId,
      };

      // const jsonParam = JSON.stringify(params)

      removeCustomerGroup(params, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("保存成功");
          this.search(0);
          this.search(1);
        } else {
          this.showErrMsg("保存失败：" + v.errmsg, "danger");
        }
      });
    },
    prevPage0: function () {
      this.sc0.pageNo = this.sc0.pageNo - 1;
      if (this.sc0.pageNo < 1) this.sc0.pageNo = 1;
      this.search(0);
    },
    nextPage0: function () {
      this.sc0.pageNo = this.sc0.pageNo + 1;
      this.search(0);
    },
    directPage0: function (pageNo) {
      this.sc0.pageNo = pageNo;
      this.search(0);
    },
    prevPage1: function () {
      this.sc1.pageNo = this.sc1.pageNo - 1;
      if (this.sc1.pageNo < 1) this.sc1.pageNo = 1;
      this.search(1);
    },
    nextPage1: function () {
      this.sc1.pageNo = this.sc1.pageNo + 1;
      this.search(1);
    },
    directPage1: function (pageNo) {
      this.sc1.pageNo = pageNo;
      this.search(1);
    },
  },
};
</script>
